/*
 * Created by JFormDesigner on Sun Nov 03 16:22:37 CST 2024
 */

package com.dhx.view.RuKu;

import com.dhx.model.GysInfo;
import com.dhx.model.RuKuDetail;
import com.dhx.model.RuKuMain;
import com.dhx.model.SpInfo;
import com.dhx.service.GysInfoService;
import com.dhx.service.RuKuService;
import com.dhx.service.SpInfoService;
import com.dhx.util.StringUtil;

import java.awt.event.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/**
 * @author 25188
 */
public class AddRuKuFrm extends JFrame {
    private GysInfoService gysInfoService = new GysInfoService();
    private SpInfoService spInfoService = new SpInfoService();
    private RuKuService ruKuService = new RuKuService();
    private TableColumn tableColumn;
    public AddRuKuFrm() {
        initComponents();
        fillComboBox();
    }

    private void fillComboBox() {
        ArrayList<GysInfo> gysInfoName = gysInfoService.getGysInfoName(new GysInfo());
        for(GysInfo gysInfo:gysInfoName){
            comboBox1.addItem(gysInfo.getName());
        }
        //添加第一列下拉框
        JComboBox<SpInfo> comboBox = new JComboBox<>();
        ArrayList<SpInfo> spInfoByName = spInfoService.getSpInfoByName(new SpInfo());
        for(SpInfo spInfo:spInfoByName){
            comboBox.addItem(spInfo);
        }

        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        tableColumn = table1.getColumnModel().getColumn(0);
        tableColumn.setCellEditor(new DefaultCellEditor(comboBox));
    }

    private void addNewbtn(ActionEvent e) {
        // TODO add your code here  给表格添加新行
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        model.addRow(new Vector<>());

    }

    private void subRKbtn(ActionEvent e) {
        // TODO add your code here  提交数据
        RuKuMain ruKuMain = new RuKuMain();
        ruKuMain.setRukuname(textField1.getText());
        ruKuMain.setGysName(comboBox1.getSelectedItem().toString());
        //遍历表格的每一行
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        ArrayList<RuKuDetail> rukudetailList = new ArrayList<>();
        BigDecimal totalPrice = new BigDecimal(0);
        int rowCount = model.getRowCount();
        for(int i=0;i<rowCount;i++){
            //获取当前行数据
            SpInfo spInfo = (SpInfo) table1.getValueAt(i,0);
            if (spInfo==null){
                continue;
            }
            if (StringUtil.isEmpty(spInfo.getName())){
                continue;
            }

            String s = table1.getValueAt(i,1).toString();
            Integer num = Integer.parseInt(s);
            RuKuDetail rukudetail = new RuKuDetail();
            rukudetail.setSpName(spInfo.getName());
            rukudetail.setNum(num);
            rukudetail.setPrice(spInfo.getPrice());
            rukudetailList.add(rukudetail);
            totalPrice = totalPrice.add(spInfo.getPrice().multiply(new BigDecimal(num)));
        }
        ruKuMain.setRukudetailList(rukudetailList);
        ruKuMain.setPrice(totalPrice);
        boolean b = ruKuService.addRuKu(ruKuMain);
        if (b){
            JOptionPane.showMessageDialog(null,"添加成功");
            dispose();
        }else{
            JOptionPane.showMessageDialog(null,"添加失败");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        textField1 = new JTextField();
        comboBox1 = new JComboBox();
        addNewbtn = new JButton();
        subRKbtn = new JButton();
        label2 = new JLabel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();

        //======== this ========
        setTitle("\u6dfb\u52a0\u5165\u5e93\u5355");
        var contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u5165\u5e93\u540d\u79f0");

        //---- addNewbtn ----
        addNewbtn.setText("\u6dfb\u52a0\u65b0\u884c");
        addNewbtn.addActionListener(e -> addNewbtn(e));

        //---- subRKbtn ----
        subRKbtn.setText("\u63d0\u4ea4\u5165\u5e93");
        subRKbtn.addActionListener(e -> subRKbtn(e));

        //---- label2 ----
        label2.setText("\u4f9b\u5e94\u5546\u9009\u62e9");

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null},
                },
                new String[] {
                    "\u5546\u54c1", "\u6570\u91cf"
                }
            ));
            scrollPane1.setViewportView(table1);
        }

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addContainerGap(27, Short.MAX_VALUE)
                    .addComponent(label1, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
                    .addGap(30, 30, 30)
                    .addComponent(label2)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(comboBox1, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(addNewbtn)
                    .addGap(18, 18, 18)
                    .addComponent(subRKbtn)
                    .addGap(55, 55, 55))
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(37, 37, 37)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 588, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(63, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(17, 17, 17)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(addNewbtn)
                        .addComponent(subRKbtn)
                        .addComponent(comboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label1))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 174, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(132, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JTextField textField1;
    private JComboBox comboBox1;
    private JButton addNewbtn;
    private JButton subRKbtn;
    private JLabel label2;
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
